package com.aierjun.line;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.EditText;

import com.aierjun.notepadjun.R;


public class UnderLineEditText extends EditText{

    private Paint linePaint;
    private int underLineColor = Color.GRAY;
    private int underLineHeight = 1;
    private int underlineHeightOffset = 0;

    public UnderLineEditText(Context context) {
        this(context,null);
    }

    public UnderLineEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UnderLineEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        underlineHeightOffset = (int)(0.19753087f * getLineHeight() + 0.5f);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.UnderLineEditText);
        underLineColor = ta.getColor(R.styleable.UnderLineEditText_underLineColor,underLineColor);
        underLineHeight = ta.getDimensionPixelSize(R.styleable.UnderLineEditText_underLineHeight,underLineHeight);
        underlineHeightOffset = ta.getDimensionPixelSize(R.styleable.UnderLineEditText_underlineHeightOffset, underlineHeightOffset);
        ta.recycle();

        linePaint = new Paint();
        linePaint.setStrokeWidth(underLineHeight);
        linePaint.setColor(underLineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);
//        PathEffect pathEffect = new DashPathEffect(new float[]{5,5,5,5},1);
//        linePaint.setPathEffect(pathEffect);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int lineCount = getLineCount();
        int height = getHeight();
        int lineHeight = getLineHeight();
        lineCount = Math.max(lineCount,1 + height / lineHeight);
        int lineY = getCompoundPaddingTop() + underlineHeightOffset;
        for (int i = 0;i <= lineCount; i++) {
            lineY += lineHeight;
//            Path path = new Path();
//            path.moveTo(getCompoundPaddingLeft(), lineY);
//            path.lineTo(getWidth() - getCompoundPaddingRight(), lineY);
//            canvas.drawPath(path, this.linePaint);
            drawWaveLine(canvas,getCompoundPaddingLeft(), lineY,getWidth() - getCompoundPaddingRight(),10f,linePaint);
            canvas.save();
        }

        super.onDraw(canvas);
    }


    private void drawWaveLine(Canvas canvas,float startX,float startY,float endX,float radius,Paint paint){
        float x1 = Math.min(startX,endX);
        float x2 = Math.max(startX,endX);
        float delta = (float) (Math.sqrt(2) / 2 * radius);
        int n = (int) ((x2 - x1) / delta / 2 + 0.5f);
        float curX,curY,curStartAngle;
        for(int i = 0; i < n; i++){
            curX = x1 + (2 * i + 1) * delta;
            curY = startY + (i % 2 == 0 ? delta : -delta);
            curStartAngle = i % 2 == 0 ? 225f : 45f;
            drawArc(canvas,curX,curY,radius,curStartAngle,90f,paint);
        }
    }

    private void drawArc(Canvas canvas,float originX,float originY,float radius,float startAngle,float sweepAngle,Paint paint){
        RectF rectF = new RectF(originX - radius,originY - radius,originX + radius,originY + radius);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,paint);
    }
}
